<!DOCTYPE HTML>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>AUI快速完成布局</title>
    <link rel="stylesheet" type="text/css" href="../css/aui.css" />


</head>

<body>
  <div>
    <header class="aui-bar aui-bar-nav aui-bar-light" id="header">
        <a class="aui-pull-left aui-btn" tapmode onclick="api.closeWin();">
            <span class="aui-iconfont aui-icon-left" style="color:#ff4285"></span>
        </a>
        <div class="aui-title" style="color:#ff4285">商品详情</div>
        <!-- <a class="aui-pull-right" tapmode>
            <span style="color:#ff4285">分享</span>
        </a> -->
    </header>
    <footer class="aui-bar aui-bar-tab" id="footer" style="background:#ff4285;color:#fff;line-height:55px;" onclick="fnTryGoods()">
        <div>免费试用</div>
    </footer>
  </div>

</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="./conf/config.js"></script>
<script type="text/javascript" src="./js/common.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript">
    apiready = function() {

        initEventListenner(); //退出键添加提示框
        //var header = document.querySelector('#header');
        $api.fixStatusBar($api.dom('header'));
        api.setStatusBarStyle({
            style: 'dark',
           color:'transparent'
        });
          // $api.fixStatusBar(header);
        var header = $api.byId('header');
        //$api.fixStatusBar(header);
        var headerH = $api.offset(header).h;


        var footer = $api.byId('footer');
        var footerH = $api.offset(footer).h;
        api.openFrame({
            name: 'ware_frame',
            url: './ware_frame.html',
            rect: {
                x: 0,
                y: headerH,
                w: 'auto',
                h: api.winHeight - footerH - headerH,
            },
            pageParam: {
                wareId: api.pageParam.wareId
            },
            bounces: true,

        });

    };

    function fnGetTaobaoList(searchType) {
      //alert(searchType);return false;
      //alert(type);

      if(searchType=='3'){
          var searchType1=1;
        var typeName="京东";
        var winName="jingdong_bind";
      }else{
        var searchType1=0;
        var typeName="淘宝";
          var winName="taobao_bind";
      }

        var userInfo = $api.getStorage("userInfo");
        if (userInfo) {
            var userid = userInfo.userid;
            var random = userInfo.random;
            var type = searchType1;
        } else {
            var userid = "";
            var random = "";
            var type = searchType1;
        }

        api.ajax({
            url: SITE_URL + 'index.php?m=api&c=appnew&a=get_tbaccount',
            method: 'post',
            data: {
                values: {
                    "userid": userid,
                    "random": random,
                    "type": type
                }
            }
        }, function(ret, err) {

            //fnUpdataListTaobaoBind(ret);
            switch (ret.status) {
                case 0:
                    alert(ret.msg);

                    break;
                case 1:

                    if (ret.count > 0) {
                        fnListtaobao(ret,searchType1);
                    } else {
                        api.openWin({
                            name: winName,
                            url: './'+winName+'.html',
                            pageParam: {
                                name: 'test'
                            }
                        });
                    };
                    break;
                case 2:
                    alert(ret.msg);
                    api.openWin({
                        name: 'login',
                        url: './login.html',
                        pageParam: {
                            name: 'test'
                        }
                    });

                    //然后关闭当前网页，跳转到另一个网页；
                    break;
                case 5:


                alert("请先绑定"+typeName+"账号！");

                api.openWin({
                    name: winName,
                    url: './'+winName+'.html',
                    pageParam: {
                        name: 'test'
                    }
                });
                    break;
            }

        });
    }

    function fnListtaobao(ret,searchType) {
    //alert(JSON.stringify(ret));
      var taobao_arr=[];
      var taobao_id=[];
      for(var i=0;i<ret.lists.length;i++){
        taobao_arr.push(ret.lists[i].account);
        taobao_id.push(ret.lists[i].id);
      }
      if(searchType=='1'){
        var typeName="京东";

      }else{
        var typeName="淘宝";

      }
//alert(taobao_arr);
      api.actionSheet({
          title: '请选择'+typeName+'账号：',
          buttons: taobao_arr
      }, function(ret, err) {
          var index = ret.buttonIndex;
          //alert(parseInt(index-1));
          //获取绑定的淘宝号id
          fnApplyVal(taobao_id[parseInt(index-1)]);
      });
    }
function fnApplyVal(taobaoId){
  var userInfo = $api.getStorage("userInfo");

      var userid = userInfo.userid;
      var random = userInfo.random;
      var goods_id = api.pageParam.wareId;
      var bind_taobao=taobaoId;

  api.ajax({
      url: SITE_URL + 'index.php?m=api&c=appnew&a=apply_order',
      method: 'post',
      data: {
          values: {
              "userid": userid,
              "random": random,
              "goods_id": goods_id,
              "bind_taobao": taobaoId,
          }
      }
  }, function(ret, err) {
    switch (ret.status) {
        case 0:
            alert(ret.msg);

            break;
        case 1:
          alert("申请成功，再看看其它商品吧！");
          api.openWin({
              name: 'main',
              url: './main.html',
              pageParam: {
                  name: 'test'
              }
          });

            break;
        case 2:
            alert(ret.msg);
            api.openWin({
                name: 'login',
                url: './login.html',
                pageParam: {
                    name: 'test'
                }
            });

            //然后关闭当前网页，跳转到另一个网页；
            break;
    }
  })
}
    function fnTryGoods() {
        //判断用户是否登录
        //alert(api.pageParam.searchType);

        fnGetTaobaoList(api.pageParam.searchType);
        // var is_login =$api.getStorage("userInfo");
        // if (is_login) {
        //     var is_zhifubao_bind=false;
        //     if(is_zhifubao_bind){
        //
        //     }else{
        //       api.confirm({
        //           title: '提示：',
        //           msg: '请绑定淘宝账号，点击确定，马上去绑定！',
        //           buttons: ['确定', '取消']
        //       }, function(ret, err) {
        //
        //         var index = ret.buttonIndex;
        //         if(index==1){
        //           //alert('我是支付宝页面');
        //           api.openWin({
        //               name: 'taobao_bind',
        //               url: './taobao_bind.html',
        //               pageParam: {
        //                   name: 'test' //给登录页面传递参数
        //               }
        //           });
        //         }
        //       });
        //     }
        //发送一个ajax请求到接口，查看是否进行了支付宝验证；
        // api.ajax({
        //     url: SITE_URL+'index.php?m=api&c=appnew&a=goods_show&goods_id=' + goodId,
        //     method: 'get',
        // }, function(ret, err) {
        //     if (ret) {
        //       //如果可以请求接口成立
        //
        //
        //     } else {
        //         alert(JSON.stringify(err));
        //     }
        // });
        //如果没有绑定支付宝就弹出一个页面

        // } else {
        //     api.confirm({
        //         title: '提示：',
        //         msg: '请先登录app',
        //         buttons: ['确定', '取消']
        //     }, function(ret, err) {
        //
        //       var index = ret.buttonIndex;
        //       if(index==1){
        //         api.openWin({
        //             name: 'login',
        //             url: './login.html',
        //             pageParam: {
        //                 name: 'test' //给登录页面传递参数
        //             }
        //         });
        //       }
        //     });
        //
        // }
    }

    function initEventListenner() {

        api.addEventListener({
            name: 'keyback'
        }, function(ret, err) {
            api.closeWin();
        });
    };
</script>

</html>
